/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cpasl;

import java.awt.Color;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.stage.Screen;

/**
 *
 * @author seanm_000
 */
public class PictureChoiceGroup extends GridPane {

    private static final Color BACKGROUND = new Color(131, 131, 131);

    private ArrayList<Button> buttons = new ArrayList<Button>();
    
    private Groups next = Groups.RANDOM_VIDEO;
    
    public PictureChoiceGroup() {
        setAlignment(Pos.CENTER);
     
        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
//        setBackground(BACKGROUND);
        int buttonWidth = ((int) primaryScreenBounds.getWidth() / CpaslManager.getInstance().getFileCount()) - 20;
        int buttonHeight = buttonWidth;
        int x = 0;
        int y = ((int) primaryScreenBounds.getHeight() / 2) - (buttonHeight / 2);
//        GridPane buttonBox = new GridPane();
//        buttonBox.setPrefSize((int) primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
        
    
        DecimalFormat numberFormat = new DecimalFormat("000");
        for (int i = 0; i < CpaslManager.getInstance().getFileCount(); i++) {
            int fileNumber = i + 1;
            String imageFilePath = CpaslManager.getInstance().getImagesDirectory().getAbsolutePath() + "/Hand-" + numberFormat.format(fileNumber) + ".jpg";
            File imageFile = new File(imageFilePath);
            Image image = new Image(imageFile.toURI().toString());
            ImageView imageView = new ImageView(image);
            imageView.setFitWidth(buttonWidth);
            imageView.setSmooth(true);
            imageView.setCache(true);
            Button imageButton = new Button("", imageView);
            imageButton.setOnAction(new ColorChoiceActionListener(fileNumber));
            ColumnConstraints column = new ColumnConstraints();
            column.setPercentWidth(50);
            getColumnConstraints().add(column); // each get 50% of width
            add(imageButton, i, 0);
            buttons.add(imageButton);
            x += buttonWidth;
        }
    }
    
    public void setNextGroup(Groups nextGroup) {
        next = nextGroup;
    }

    private class ColorChoiceActionListener implements EventHandler<ActionEvent> {
        
        private int fileNumber = 0;
        
        public ColorChoiceActionListener(int fileNumber) {
            this.fileNumber = fileNumber;
        }

        @Override
        public void handle(ActionEvent e) {
            CpaslManager.getInstance().writePictureChoice(fileNumber);
            CpaslManager.getInstance().switchToDisplay(next, 500);
        }

    }
    
}
